package com.glela.platform.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glela.cache.redis.RedisKeys;
import com.glela.cache.redis.RedisUtil;
import com.glela.platform.mapper.MoneyBusinessTypeMapper;
import com.glela.platform.model.MoneyBusinessType;
import com.glela.platform.service.MoneyBusinessTypeService;

/**
 * 货币服务类
 * 
 * @author gaoweiqi
 * @date 2017年8月19日 12:44:05
 */
@Service
public class MoneyBusinessTypeServiceImpl implements MoneyBusinessTypeService {
	
	protected static Logger logger = Logger.getLogger(BaseRestrictionsServiceImpl.class);
	@Autowired
	private MoneyBusinessTypeMapper moneyBusinessTypeMapper;
	@Autowired
	private RedisUtil				redisUtil;
	/**
	 * 查找所有货币类型分类 key:typeID value:OBJ
	 * @return Map<Integer, MoneyBusinessType> key:typeID value:OBJ
	 * @author gaoweiqi
	 */
	public Map<Integer, MoneyBusinessType> selectAllMoneyBusinessType() {
		// 查找所有的货币业务类型分类
		List<MoneyBusinessType> MoneyBusinessTypeList = moneyBusinessTypeMapper
				.selectALL();
		Map<Integer, MoneyBusinessType> MoneyBusinessTypeMap = new HashMap<Integer, MoneyBusinessType>();
		for (MoneyBusinessType temp : MoneyBusinessTypeList) {
			MoneyBusinessTypeMap.put(temp.getTypeId(), temp);
		}
		return MoneyBusinessTypeMap;
	}

	/**
	 *  根据类型ID在DB中查找货币分类类型
	 * @return MoneyBusinessType
	 * @parpa int type
	 * @author gaoweiqi
	 */
	public MoneyBusinessType selectMoneyBusinessByTypeId(int type) {
		MoneyBusinessType temp=moneyBusinessTypeMapper.selectByTypeID(type);
		return temp;
	}
	public MoneyBusinessType selectMoneyBusinessByTypeIdForCache(int type) {
		return getMoneyBusinessTypeForCache(type);
	}
	public MoneyBusinessType getMoneyBusinessTypeForCache(int type) {
		String cacheValue = redisUtil.hget(RedisKeys.YG_SYSTEM_MONEY_BUSINESS_TYPE, String.valueOf(type));
		try {
			if(StringUtils.isBlank(cacheValue)) {
				MoneyBusinessType businessType = selectMoneyBusinessByTypeId(type);
				if(businessType != null) {
					redisUtil.hmset(RedisKeys.YG_SYSTEM_MONEY_BUSINESS_TYPE, String.valueOf(type), JSONObject.toJSONString(businessType));
				}
				return businessType;
			}else {
				return JSON.parseObject(cacheValue, MoneyBusinessType.class);
			}
		} catch (Exception e) {
			logger.error("MoneyBusinessType类型转换错误: " + e);
			return null;
		}
	}
}
